from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time
import random
import json
from urllib.request import urlopen
from urllib.parse import urlparse


# driver = webdriver.PhantomJS()
# # url = 'https://class.imooc.com/datastructure#Anchor'
# url = 'https://class.imooc.com/sale/datastructure'
# driver.get(url)
# # driver.maximize_window()
# # time.sleep(2)
# driver.save_screenshot('baidu1' +str(random.randint(0, 999999))+ '.png')
# driver.close()


class GeekTime:

    def __init__(self):
        file_name = './data/' + time.strftime("%Y-%m-%d", time.localtime()) + '-geekTime-data'
        self.f = open(file_name, 'a')

        self.driver = webdriver.Chrome()
        self.url = 'https://time.geekbang.org/'
        self.driver.maximize_window()
        self.driver.get(self.url)
        time.sleep(5)

    def has_element_by_class_name(self, class_name):
        try:
            element = self.driver.find_element_by_class_name(class_name)
            return True, element
        except:
            return False, None

    def mouse_scroll(self):
        # js = 'document.documentElement.scrollTop=10000'
        # self.driver.execute_script(js)
        # time.sleep(3)
        for i in range(5):
            self.driver.find_element_by_tag_name('body').send_keys(Keys.END)
            time.sleep(0.2)

    def main(self):

        has_ad, ad_element = self.has_element_by_class_name('_1TwiVsuM_0')
        if has_ad:
            ad_element.click()
            time.sleep(1)

        while True:
            self.mouse_scroll()
            has_element, ele = self.has_element_by_class_name('vUoikev7_0')
            if has_element:
                # self.mouse_scroll()
                ele.click()
                time.sleep(1)
                continue

            has_element, ele = self.has_element_by_class_name('OjL5wNoM_0')
            if has_element:
                break
        self.parse_course()
        time.sleep(5)

    def parse_course(self):
        courses = self.driver.find_elements_by_class_name('_20Cq3Rn7_0')
        for course in courses:
            course_info = {
                'course_title': course.find_element_by_tag_name('h2').text,
                'course_url': course.find_element_by_class_name('_1miPDP4s_0').get_attribute('src'),
                'course_img_url': course.find_element_by_class_name('_1miPDP4s_0').get_attribute('src'),
                'course_teacher_name': course.find_element_by_class_name('_33lENDr7_0').text,
                'course_teacher_header_url': '',
                'course_sale_screenshot': '',
            }
            try:
                course_info['course_price'] = course.find_element_by_class_name('_1EwQIhcU_0').text.replace('原价',
                                                                                                            '').replace(
                    '¥',
                    '').strip(),
            except:
                course_info['course_price'] = course.find_element_by_class_name('_1BSc9YvC_0').text.replace('原价',
                                                                                                            '').replace(
                    '¥',
                    '').strip(),
            self.write(course_info)
            self.download_img(course_info['course_img_url'])

    def write(self, data):
        self.f.write(json.dumps(data) + '\n')

    def download_img(self, url):

        file_name = 'course_images/geek_time/' + urlparse(url).path.split('/')[-1]

        data = urlopen(url).read()
        with open(file_name, 'wb') as img:
            img.write(data)
            img.close()
        time.sleep(0.5)

    def __del__(self):
        self.driver.close()
        self.f.close()


geek_time = GeekTime()
geek_time.main()
